<template>
  <div class="container">
    <el-button
      size="small"
      type="primary"
      @click="addActivationCode"
      style="margin-bottom: 20px; float: right"
      >生成激活码
    </el-button>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      :row-class-name="xxxTableRowClassName"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }"
    >
      <el-table-column
        prop="activationCode"
        label="激活码"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="schoolName"
        label="所属学校"
        width="width"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="status"
        label="激活码状态"
        width="width"
        align="center"
      >
        <template slot-scope="scope">
          <span>
            {{ scope.row.status === "1" ? "已启用" : "未启用" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="生成时间"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="useTime"
        label="使用时间"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="danger"
            size="small"
            plain
            @click="delActivationCode($event, row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="生成激活码"
      :visible.sync="dialogFormVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="left"
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="140px"
      >
        <el-form-item label="学校列表:" prop="schoolId">
          <el-select
            v-model="form.schoolId"
            placeholder="请选择学校"
            style="width: 80%"
          >
            <el-option
              v-for="(item,index) in schoolList"
              :key="index"
              :label="item.shName"
              :value="item.shId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button :loading="disable" size="small" type="primary" @click="save"
          >{{ disable ? "加载中" : "确 定" }}
        </el-button>
      </div>
    </el-dialog>

    <pagination-page @size="changeSize" @page="changePage" :total="total" />
  </div>
</template>

<script>
import {
  selectSchoolInfo,
  selectSchool,
  findSchoolInfo,
  deleteSchoolInfo,
} from "@/api/dataoverview";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      disable: false,
      total: -1,
      loading: false,
      page: {
        pageSize: 10,
        pageIndex: 1,
      },
      dialogFormVisible: false,
      schoolList: [],
      tableData: [],
      form: {
        schoolId: "",
      },
      rules: {
        schoolId: [
          {
            required: true,
            message: "请选择学校",
          },
        ],
      },
      isLoading: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getActivationCodeList();
    this.getSelectSchool();
  },
  mounted() {},
  methods: {
    getActivationCodeList() {
      let data = {
        ...this.page,
      };
      this.isLoading = true;
      selectSchoolInfo(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result;
          this.total = res.total;
          this.isLoading = false;
        }
      });
    },
    getSelectSchool() {
      let data = {
        userId: this.$store.state.user.USER_INFO.id,
      };
      selectSchool(data).then((res) => {
        this.schoolList = res.result;
      });
    },
    addActivationCode() {
      this.dialogFormVisible = true;
      (this.form = {
        schoolId: "",
      }),
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate();
        });
    },
    delActivationCode(evt, row) {
      let target = evt.target;
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          let data = {
            schoolId: row.schoolId,
            sid:row.id,
            state:row.status
          };
          deleteSchoolInfo(data).then((res) => {
            if (res.code === 200) {
              this.getActivationCodeList();
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    save() {
      let data = {
        ...this.form,
      };
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.disable = true;

          findSchoolInfo(data).then((res) => {
            if (res.code === 200) {
              this.dialogFormVisible = false;
              this.getActivationCodeList();
              this.$message.success("保存成功");
              this.disable = false;
            } else {
              this.disable = false;
              this.dialogFormVisible = false;
              this.$message.error(res.message);
            }
          });
        } else {
          return false;
        }
      });
    },
    xxxTableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    changeSize(size) {
      this.page.pageSize = size;
      this.getActivationCodeList();
    },
    changePage(page) {
      this.page.pageIndex = page;
      this.getActivationCodeList();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
::v-deep .el-upload-list__item {
  display: none;
}
::v-deep .el-form-item__label {
  padding-left: 40px;
}
</style>
